package com.trip.api;

import com.trip.config.AppUser;
import com.trip.service.PlaceAndExperienceService;
import com.trip.util.AuthUtil;
import com.trip.vo.*;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by hc24 on 2017/9/29.
 */

@Api("Place&Experience")
@RequestMapping("pae")
@RestController
public class PlaceAndExperienceController {

    @Autowired
    private PlaceAndExperienceService placeAndExperienceService;

    @AppUser
    @ApiOperation("Sign in Place or Experience")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "paeId", value = "The ID of Place or Experience", required = true, dataType = "string",paramType = "path")
    })
    @RequestMapping(value = "/{paeId}/sign",method = RequestMethod.POST)
    public ControllerResult signIn(@RequestHeader @ApiParam(name = "authToken", value = "Auth Token",required = true)String authToken,
                                   @PathVariable String paeId){

        placeAndExperienceService.signIn(AuthUtil.getUserId(authToken),paeId);
        return ControllerResult.success();
    }

    @AppUser
    @ApiOperation("Cancel sign in Place or Experience")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "paeId", value = "The ID of Place or Experience", required = true, dataType = "string",paramType = "path")
    })
    @RequestMapping(value = "/{paeId}/sign",method = RequestMethod.DELETE)
    public ControllerResult cancelSignIn(@RequestHeader @ApiParam(name = "authToken", value = "Auth Token",required = true)String authToken,
                                   @PathVariable String paeId){

        placeAndExperienceService.cancelSignIn(AuthUtil.getUserId(authToken),paeId);
        return ControllerResult.success();
    }

    @AppUser
    @ApiOperation("Write note")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "paeId", value = "The ID of Place or Experience", required = true, dataType = "int",paramType = "path")
            //@ApiImplicitParam(name = "note", value = "Note content", required = true,paramType = "form")
    })
    @RequestMapping(value = "/{paeId}/note",method = RequestMethod.POST)
    public ControllerResult createNote(@RequestHeader @ApiParam(name = "authToken", value = "Auth Token",required = true) String authToken,
                                       @PathVariable String paeId,
                                       @RequestBody SingleVo<String> note){

        placeAndExperienceService.createNote(AuthUtil.getUserId(authToken),paeId,note.getValue());
        return ControllerResult.success();
    }

    @AppUser
    @ApiOperation("Tap star and write review")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "paeId", value = "The ID of Place or Experience", required = true, dataType = "int",paramType = "path")
           // @ApiImplicitParam(name = "star", value = "Star point", required = true, dataType = "String",paramType = "form"),
            //@ApiImplicitParam(name = "content", value = "Review Content", required = true, dataType = "String",paramType = "form")
    })
    @RequestMapping(value = "/{paeId}/review",method = RequestMethod.POST)
    public ControllerResult createReview(@RequestHeader @ApiParam(name = "authToken", value = "Auth Token",required = true)String authToken,
                                         @PathVariable String paeId,
                                         @RequestBody @ApiParam("Review Request Object") ReviewRequest reviewRequest){

        placeAndExperienceService.createReview(AuthUtil.getUserId(authToken),paeId,reviewRequest.getStar(),reviewRequest.getReviewContent());
        return ControllerResult.success();
    }

    @ApiOperation("Get detail info by ID")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "paeId", value = "The ID of Place or Experience", required = true, dataType = "int",paramType = "path")
    })
    @RequestMapping(value = "/{paeId}",method = RequestMethod.GET)
    public ControllerResult<PlaceAndExperienceDetailVo> queryDetail(@RequestHeader(required = false) @ApiParam(name = "authToken", value = "Auth Token") String authToken,
                                                                   @PathVariable  String paeId){
        Integer userId=null;
        try{
            userId=AuthUtil.getUserId(authToken);
        }catch (Exception e){}
        PlaceAndExperienceDetailVo vo= placeAndExperienceService.queryDetail(paeId,userId);
        //vo.setStar(Math.floor(vo.getStar()));
        return ControllerResult.success(placeAndExperienceService.queryDetail(paeId,userId));
    }

    @ApiOperation("Query Place or Experience list by state or tag")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "start",value = "from 0",required = true, dataType = "int",paramType = "query",defaultValue = "0"),
            @ApiImplicitParam(name = "pageSize",  required = true, dataType = "int",paramType = "query",defaultValue = Page.DEFAULT_PAGE_SIZE+"")
    })
    @RequestMapping(value = "",method = RequestMethod.GET)
    public ControllerResult<Page<PlaceAndExperienceListItemVo>> queryList(@RequestHeader(required = false) @ApiParam(name = "authToken", value = "Auth Token") String authToken,
                                                                          @ModelAttribute PlaceAndExperienceQo qo, int start, int pageSize, BindingResult bindingResult){
        Integer userId=null;
        try{
            userId=AuthUtil.getUserId(authToken);
        }catch (Exception e){}
        qo.setUserId(userId);
        return ControllerResult.success(placeAndExperienceService.queryList(qo,start,pageSize));
    }


    @ApiOperation("Nearby Place or Experience")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lon",value = "Longitude",required = true, dataType = "double",paramType = "query"),
            @ApiImplicitParam(name = "lat",value = "Latitude", required = true, dataType = "double",paramType = "query"),
            @ApiImplicitParam(name = "dis",value = "Distance", required = true, dataType = "double",paramType = "query")
    })
    @RequestMapping(value = "/nearby",method = RequestMethod.GET)
    public ControllerResult<List<PlaceAndExperienceListItemVo>> queryNearby(@RequestHeader(required = false) @ApiParam(name = "authToken", value = "Auth Token") String authToken,
              Double lon, Double lat, @RequestParam(defaultValue = "50") Double dis){
        Integer userId=null;
        try{
            userId=AuthUtil.getUserId(authToken);
        }catch (Exception e){}
        PlaceAndExperienceQo qo=new PlaceAndExperienceQo();
        qo.setUserId(userId);
        qo.setTags(new String[]{"C"});
        return ControllerResult.success(placeAndExperienceService.queryNearby(lon,lat,dis,userId));
        //return ControllerResult.success(placeAndExperienceService.queryList(qo,0,10).getRows());

    }

    static class ReviewRequest {
        private Integer star;
        private String reviewContent;

        public Integer getStar() {
            return star;
        }

        public void setStar(Integer star) {
            this.star = star;
        }

        public String getReviewContent() {
            return reviewContent;
        }

        public void setReviewContent(String reviewContent) {
            this.reviewContent = reviewContent;
        }
    }
}
